import { IMTableType } from "@/types/table";
import { password_validate, password_new_confirm_validate } from "./utils";
import { IMOptionsType } from "@/types/dict";
export const operateType = ref<any>("");
export const businessStateMap: IMOptionsType[] = [
  { label: getProxyT("dict6"), value: "" },
  { label: getProxyT("dict7"), value: 0 },
  { label: getProxyT("dict8"), value: 1 },
];
export const businessStatusMapA: IMOptionsType[] = [
  { label: getProxyT("dict6"), value: "Infinity" },
  { label: getProxyT("dict7"), value: 0 },
  { label: getProxyT("dict8"), value: 1 },
];
export const businessStatusMapB: IMOptionsType[] = [
  { label: getProxyT("dict7"), value: 0 },
  { label: getProxyT("dict8"), value: 1 },
];
export const columns = reactive<Array<IMTableType>>([
  { title: getProxyT("base.index"), dataIndex: "index", key: "index" },
  {
    title: computed(() => `${$t("table58")}ID`),
    dataIndex: "uuid",
    key: "uuid",
  },
  { title: getProxyT("table59"), dataIndex: "name", key: "name" },
  { title: getProxyT("table60"), dataIndex: "username", key: "username" },
  {
    title: getProxyT("table61"),
    dataIndex: "start_date",
    key: "start_date",
  },
  { title: getProxyT("table62"), dataIndex: "end_date", key: "end_date" },
  { title: getProxyT("table63"), dataIndex: "status", key: "status" },
  {
    title: getProxyT("table43"),
    dataIndex: "created_at",
    key: "created_at",
    // customRender: ({ text }: any) => getTimeByZone(text),
  },
  {
    title: getProxyT("table64"),
    dataIndex: "analysis_name",
    key: "analysis_name",
  },
  { title: getProxyT("base.action"), key: "action" },
]);

export const name_help = ref<any>(
  <i class="fs10">{$t("placeholder7") + "2-12" + $t("placeholder10")}</i>
);
export const username_help = ref<any>(
  <i class="fs10">{$t("placeholder9") + "4-12" + $t("placeholder170")}</i>
);
export const password_help = ref<any>(
  <i class="fs10">{$t("placeholder11")}</i>
);
export const password_confirm_help = ref<any>(
  <i class="fs10">{$t("placeholder12")}</i>
);
export const drawerList = reactive<any>([
  {
    label: getProxyT("table59"),
    name: "name",
    help: computed(() => name_help.value),
    maxlength: 12,
    type: "input",
    model: "name",
    placeholder: getProxyT("placeholder4"),
    id: randomString(),
    rules: [
      {
        required: true,
        trigger: ["change", "blur"],
        validator: (rule: any, value: string) => {
          if (
            !(
              /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(value) &&
              value?.length >= 2 &&
              value?.length <= 12
            )
          ) {
            name_help.value = (
              <i class="fs10">
                {$t("placeholder8") + "2-12" + $t("base.digit")}
              </i>
            );
            drawerInputValidators[0] = true;
            return Promise.reject();
          }

          name_help.value = (
            <i class="fs10">
              {$t("placeholder7") + "2-12" + $t("placeholder10")}
            </i>
          );
          drawerInputValidators[0] = false;
          return Promise.resolve();
        },
      },
    ],
  },
  {
    label: getProxyT("table60"),
    name: "username",
    disabled: computed(() => operateType.value === "update"),
    id: randomString(),
    help: computed(() => username_help.value),
    maxlength: 12,
    type: "input",
    model: "username",
    placeholder: getProxyT("placeholder5"),
    rules: [
      {
        required: true,
        trigger: ["change", "blur"],
        validator: (rule: any, value: string) => {
          if (
            !(
              /^[0-9a-zA-Z_]{1,}$/.test(value) &&
              value?.length >= 4 &&
              value?.length <= 12
            )
          ) {
            username_help.value = (
              <i class="fs10">
                {$t("placeholder8") + "4-12" + $t("base.digit")}
              </i>
            );

            drawerInputValidators[1] = true;
            return Promise.reject();
          }

          username_help.value = (
            <i class="fs10">
              {$t("placeholder9") + "4-12" + $t("placeholder170")}
            </i>
          );
          drawerInputValidators[1] = false;
          return Promise.resolve();
        },
      },
    ],
  },
  {},
  {},
  {
    label: getProxyT("table68"),
    name: "start_end",
    type: "rangePicker",
    model: "start_end",
    placeholder: computed(() => [$t("base.startDate"), $t("base.endDate")]),
    drawerClass: "drawer-item-width-auto",
    rules: [
      {
        required: true,
        message: getProxyT("placeholder15"),
      },
    ],
  },
  {
    label: getProxyT("table63"),
    name: "status",
    type: "radio",
    model: "status",
    options: businessStatusMapB,
    placeholder: getProxyT("placeholder3"),
    rules: [
      {
        required: true,
        message: getProxyT("placeholder3"),
      },
    ],
  },
]);
watch([() => operateType.value], (newArrState) => {
  if (newArrState[0] === "update") {
    drawerList[2] = {};
    drawerList[3] = {};
  } else {
    drawerList[2] = {
      label: getProxyT("table69"),
      name: "password",
      help: computed(() => password_help.value),
      maxlength: 13,
      type: "inputPassword",
      model: "password",
      placeholder: computed(
        () => `${$t("placeholder6")}8-13${$t("placeholder104")}`
      ),
      rules: [
        {
          required: true,
          validator: password_validate,
          trigger: ["change", "blur"],
        },
      ],
    };
    drawerList[3] = {
      label: getProxyT("table70"),
      name: "password_confirmation",
      maxlength: 13,
      help: computed(() => password_confirm_help.value),
      type: "inputPassword",
      model: "password_confirmation",
      placeholder: computed(
        () => `${$t("placeholder6")}8-13${$t("placeholder104")}`
      ),
      rules: [
        {
          required: true,
          validator: password_new_confirm_validate,
          trigger: ["change"],
        },
      ],
    };
  }
});

export const drawerInstance = ref<any>(null);
export const queryParams = reactive<any>({});
export const drawerInputValidators = reactive(createArray(false, 4));
export const modalInputValidators = reactive(createArray(false, 2));
